package com.yiyuzi.admin.user;

import com.yiyuzi.admin.common.domain.IDQueryDTO;
import com.yiyuzi.admin.common.constant.ResponseCodeEnum;
import com.yiyuzi.admin.common.domain.PageResultDTO;
import com.yiyuzi.admin.common.domain.ResponseDTO;
import com.yiyuzi.admin.user.domain.dto.UserRoleAddDTO;
import com.yiyuzi.admin.user.domain.dto.UserRoleDTO;
import com.yiyuzi.admin.user.domain.dto.UserRoleQueryDTO;
import com.yiyuzi.admin.user.service.UserRoleService;
import com.yiyuzi.admin.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yiyuzi
 * @date 2021/5/8 9:52
 */

@RestController
@RequestMapping("userRole")
@Api(tags = "用户-角色")
public class UserRoleController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @GetMapping("/query/{id}")
    @ApiOperation(value = "查询")
    public ResponseDTO<UserRoleDTO> getById(@PathVariable Long id){
        return ResponseDTO.res(userRoleService.findById(id), ResponseCodeEnum.NOTFOUND);
    }

    @PostMapping("/add/one")
    @ApiOperation(value = "新增")
    public ResponseDTO<String> add(UserRoleAddDTO addDTO){
        return ResponseDTO.res(userRoleService.add(addDTO));
    }

    @PostMapping("/add/list")
    @ApiOperation(value = "批量新增")
    @ResponseBody
    public ResponseDTO<String> add(@RequestBody List<UserRoleAddDTO> list){
        try {
            return ResponseDTO.res(userRoleService.add(list));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResponseDTO.err();
        }
    }

    @PostMapping("/query/all")
    @ApiOperation(value = "查询全部")
    public ResponseDTO<PageResultDTO<UserRoleDTO>> getAll(){
        return ResponseDTO.res(userRoleService.findPage(new UserRoleQueryDTO()), ResponseCodeEnum.NOTFOUND);
    }

    @PostMapping("/query/page")
    @ApiOperation(value = "条件查询")
    public ResponseDTO<PageResultDTO<UserRoleDTO>> getPage(UserRoleQueryDTO queryDTO){
        return ResponseDTO.res(userRoleService.findPage(queryDTO), ResponseCodeEnum.NOTFOUND);
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public ResponseDTO<String> deleteById(@PathVariable Long id){
        return ResponseDTO.res(userRoleService.removeById(id));
    }

    @PostMapping("/delete/list")
    @ApiOperation(value = "批量删除")
    public ResponseDTO<String> deleteList(IDQueryDTO idQueryDTO) {
        try {
            return ResponseDTO.res(userRoleService.deleteList(idQueryDTO.getIdList()));
        }catch (RuntimeException e){
            e.printStackTrace();
            return ResponseDTO.err();
        }
    }
}
